﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYRevitCode.FormUtility;

namespace YArchitech.HVAC
{
	public partial class FormInHomePipe : System.Windows.Forms.Form
	{
		public FormInHomePipe()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			List<KeyValuePair<System.Windows.Forms.Control, bool>> inputControlGroup = new List<KeyValuePair<System.Windows.Forms.Control, bool>>();
			this.m_thisManger = new ControlInfoManager(this, inputControlGroup, "{70F37132-D0EB-48B3-8FBF-DAAF9A1A6C27}", null);
			base.Icon = Resource.YArchitechIcon;
			UserSetting userSetting = new UserSetting();
			this.fromData = userSetting.GetInHomePipeSetting();
			this.InitDlg();
		}

		private void InitDlg()
		{
			InHomePipeData inHomePipeData = new InHomePipeData(this.fromData);
			if (inHomePipeData.DimStype == 1)
			{
				this.radioXTBH.Checked = true;
			}
			else
			{
				this.radioBHXT.Checked = true;
			}
			if (inHomePipeData.DimContent == 1)
			{
				this.radioAdd.Checked = true;
			}
			else
			{
				this.radioDef.Checked = true;
			}
			this.textBox1.Text = inHomePipeData.Content;
			if (string.IsNullOrEmpty(this.textBox1.Text))
			{
				this.textBox1.Text = "1";
			}
			this.checkBox1.Checked = inHomePipeData.IsLead;
			this.TextEnable();
		}

		private void UpdataFormData()
		{
			if (this.radioXTBH.Checked)
			{
				this.fromData.DimStype = 1;
			}
			else
			{
				this.fromData.DimStype = 2;
			}
			if (this.radioAdd.Checked)
			{
				this.fromData.DimContent = 1;
			}
			else
			{
				this.fromData.DimContent = 2;
			}
			this.fromData.Content = this.textBox1.Text;
			this.fromData.IsLead = this.checkBox1.Checked;
			this.TextEnable();
		}

		private void radioXTBH_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdataFormData();
		}

		private void radioAdd_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdataFormData();
		}

		private void checkBox1_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdataFormData();
		}

		private void textBox1_TextChanged(object sender, EventArgs e)
		{
			this.UpdataFormData();
			this.textBox1.Focus();
		}

		private void TextEnable()
		{
			this.textBox1.Enabled = this.radioDef.Checked;
		}

		private void FormInHomePipe_FormClosed(object sender, FormClosedEventArgs e)
		{
			new UserSetting().WriteInHomePipeSetting(this.fromData);
			this.SimulationPressESC();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SimulationPressESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			FormInHomePipe.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormInHomePipe.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void FormInHomePipe_Load(object sender, EventArgs e)
		{
			if (this.m_thisManger != null && !this.m_thisManger.SetControlInfoToForm())
			{
				base.Location = new System.Drawing.Point(230, 170);
			}
			base.KeyPreview = true;
		}

		private void FormInHomePipe_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == '\u001b')
			{
				new UserSetting().WriteInHomePipeSetting(this.fromData);
				base.Close();
			}
		}

		private void FormInHomePipe_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_thisManger != null)
			{
				this.m_thisManger.GetControlInfoToXml();
			}
		}

		public InHomePipeData fromData = new InHomePipeData();

		private ControlInfoManager m_thisManger;
	}
}
